#include "TeamHistory.h"
#include "People.h"
#include "MatchData.h"
#include "ZStream.h"

#include <cassert>

void TeamItemHistory::Serialize(ZStream& o_stream) const
{
	o_stream.WriteOpenTag("TeamItemHistory");

	o_stream.WriteAttribute("Year", m_year);
	o_stream.WriteAttribute("LeagueID", m_leagueID);
	o_stream.WriteAttribute("LeaguePosition", m_leaguePosition);
	o_stream.WriteAttribute("TopGoalPlayerID", m_topGoalPlayerID);
	o_stream.WriteAttribute("TopGoal", m_topGoal);
	o_stream.WriteAttribute("TopMomPlayerID", m_topMomPlayerID);
	o_stream.WriteAttribute("TopMom", m_topMom);
	o_stream.WriteAttribute("TopRatingPlayerID", m_topRatingPlayerID);
	o_stream.WriteAttribute("TopRating", m_topRating);

	o_stream.WriteCloseTag("TeamItemHistory");
}

void TeamItemHistory::Deserialize(ZStream& i_stream)
{
	i_stream.SkipLine();

	i_stream.ReadAttribute(m_year);
	i_stream.ReadAttribute(m_leagueID);
	i_stream.ReadAttribute(m_leaguePosition);
	i_stream.ReadAttribute(m_topGoalPlayerID);
	i_stream.ReadAttribute(m_topGoal);
	i_stream.ReadAttribute(m_topMomPlayerID);
	i_stream.ReadAttribute(m_topMom);
	i_stream.ReadAttribute(m_topRatingPlayerID);
	i_stream.ReadAttribute(m_topRating);
	
	i_stream.SkipLine();
}

TeamHistory::TeamHistory()
	:m_currentYear(0)
{
}

void TeamHistory::Init(size_t i_param, const int i_startingYear)
{
	m_totalPodium = 0;
	m_podium.resize(3,0);
	m_yearsInLeagueLayer.resize(i_param ,0);
	m_bankruptCount = 0;
	m_startingYear = i_startingYear;
}

ZStream& TeamHistory::Serialize(ZStream& o_stream) const
{
	o_stream.WriteOpenTag("TeamHistory");

	o_stream.WriteAttribute("HistoryNum", m_items.size());

	for (TeamItemHistoryVecCit it = m_items.begin(); it != m_items.end(); ++it)
	{
		it->Serialize(o_stream);
	}

	o_stream.WriteAttribute("TotalPodium", m_totalPodium);
	o_stream.WriteAttribute("BankruptCount", m_bankruptCount);

	o_stream.WriteAttribute("PodiumNum", m_podium.size());
	for(size_t i=0; i<m_podium.size(); ++i)
		o_stream.WriteAttribute("Podium", m_podium[i]);

	o_stream.WriteAttribute("LeagueLayers", m_yearsInLeagueLayer.size());
	for(size_t i=0; i<m_yearsInLeagueLayer.size(); ++i)
		o_stream.WriteAttribute("LayerYears", m_yearsInLeagueLayer[i]);

	o_stream.WriteCloseTag("TeamHistory");

	return o_stream;
}

ZStream& TeamHistory::Deserialize(ZStream& i_stream)
{
	i_stream.SkipLine();

	size_t historyNum;

	i_stream.ReadAttribute(historyNum);

	m_items.clear();
	m_items.reserve(historyNum);

	for (size_t i = 0; i < historyNum; ++i)
	{
		TeamItemHistory history;
		history.Deserialize(i_stream);
		m_items.push_back(history);
	}

	i_stream.ReadAttribute(m_totalPodium);
	i_stream.ReadAttribute(m_bankruptCount);

	size_t theNum;

	i_stream.ReadAttribute(theNum);
	m_podium.resize(theNum);
	for(size_t i = 0; i < theNum; ++i)
	{
		i_stream.ReadAttribute(m_podium[i]);
	}

	i_stream.ReadAttribute(theNum);
	m_yearsInLeagueLayer.resize(theNum);
	for(size_t i = 0; i < theNum; ++i)
	{
		i_stream.ReadAttribute(m_yearsInLeagueLayer[i]);
	}

	i_stream.SkipLine();

	return i_stream;
}

void TeamHistory::AddTeamItemHistory(const PeopleVec& i_player, size_t i_year, int i_leagueID, int i_leaguePosition, unsigned int i_minNumMatchesForTopEval)
{
	size_t idPlayerGol = static_cast<size_t>(-1);
	size_t idPlayerMom = static_cast<size_t>(-1);
	size_t idPlayerEval = static_cast<size_t>(-1);
	int maxGol = -1;
	int maxMom = -1;
	int maxEval = -1;
	bool isCurrentTopEvalValid = false;

	for(PeopleVecCit it = i_player.begin(); it < i_player.end(); ++it)
	{
		const People& player = *(*it);
		assert(player.IsPlayer());

		const int mom = player.GetHistory().GetMoM();
		if(mom > maxMom)
		{
			maxMom = player.GetHistory().GetMoM();
			idPlayerMom = player.GetId();
		}
		
		const int scoredGoal = player.GetHistory().GetScoredGoal();
		if(scoredGoal > maxGol)
		{
			maxGol = player.GetHistory().GetScoredGoal();
			idPlayerGol = player.GetId();
		}

		const int eval = player.GetHistory().GetEval();
		const bool isCurrentEvalValid = (player.GetHistory().GetMatch() >= i_minNumMatchesForTopEval);
		if(	((isCurrentEvalValid == isCurrentTopEvalValid) && (eval > maxEval)) ||
			(isCurrentEvalValid && (!isCurrentTopEvalValid)))
		{
			maxEval = eval;
			idPlayerEval = player.GetId();
		}
		isCurrentTopEvalValid = isCurrentTopEvalValid || isCurrentEvalValid;

	}

	assert(idPlayerGol != -1 && idPlayerMom != -1 && idPlayerEval != -1);
	m_items.push_back(TeamItemHistory(idPlayerGol, maxGol, idPlayerMom, maxMom, idPlayerEval, maxEval, i_year, i_leagueID, i_leaguePosition));

	++m_currentYear;
}

